<?php

class Router
{
  private $tree = NULL;
  private $path = '';
  private $sections = '';
  private $config = '';
  private $_db = null;
  private $_index = '';
  
//------------------------------------------------------------------------------
  public function __construct() {
    require_once( 'configs/tree.php' );
    $this->tree = $tree;
  }

//------------------------------------------------------------------------------
  public function init( $index, IDatabase &$db ) {
    $this->_index = $index;
    $this->_db = $db;
  }
  
//------------------------------------------------------------------------------
  public function getLocation( $url ) {

//  public function parse( $url ) {
    $url = trim( $url, '?/');
 		if($url == '') {
 			$url = 'index';
 		}
		$this->path = explode('/', $url);
		$this->sections = array_flip( $this->path );
		$parent = '';
//		print_r( $this->tree );
		$branch = $this->tree;
		$i = 0;
		foreach($this->path as $key=>$value) {
      if($value) {
        if( isset( $branch[$value] ) ) {
          $branch = $branch[$value];
        }
        else if( $i == count( $this->path )-1 ) {
          unset( $this->path[$key] );
        }
        else {
          return false;
        }
      }
      else {
        return false;
      }
      ++$i;
    }	// end foreach
  	if( !isset($this->path[0]) )
	  	return false;

	  $this->path = array_reverse($this->path);
 		$this->sections[ $this->path[0] ] = 'current';
    return $this->tree[ $this->path[0] ];
  }

//------------------------------------------------------------------------------
  public function getPath() {
    return $this->path;
  }

//------------------------------------------------------------------------------
  public function getSections() {
    return $this->sections;
  }

}

?>
